package wlt.model.acl;

import org.apache.commons.io.IOUtils;
import org.casbin.jcasbin.main.Enforcer;
import org.casbin.jcasbin.model.Model;
import org.casbin.jcasbin.persist.file_adapter.FileAdapter;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @author wuliting
 * @date 2021/11/4
 * 使用acl的模型的简单示例
**/
public class SimpleACLDemo {
    public static void main(String[] args) {
        String modelContext = "";
        try {
             modelContext = IOUtils.toString(Objects.requireNonNull(SimpleACLDemo.class.getResourceAsStream("/acl/model.conf")), StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Model model = new Model();
        model.loadModelFromText(modelContext);
        Enforcer enforcer = new Enforcer(model, new FileAdapter(System.getProperty("user.dir") + "/src/main/resources/acl/policy.csv"));

        //access control case
        String sub = "root";
        String obj = "data1";
        String act = "write";
        if (enforcer.enforce(sub,obj,act)) {
            System.out.println("校验通过，放行");
        }else {
            System.out.println("access denied");
        }
    }
}
